package core;

import static com.googlecode.javacv.cpp.opencv_core.cvGetSeqElem;

import com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.CvSeq;

public class Ball implements Comparable<Ball>{
	private CvSeq ballContour;
	private int left;
	private int right;
	private int top;
	private int bottom;
	private int centerX;
	private int centerY;
	private double radius;
	
	public Ball(CvSeq ballContour) {		
		super();
		
		this.ballContour = ballContour;		
		this.lRBT(ballContour);
		
		this.centerX = (left+right)/2;
		this.centerY = (top+bottom)/2;
		this.radius = ((right-left)/2.0 + (top-bottom)/2.0)/2.0;
	}
	
	public void lRBT(CvSeq seq){
		CvPoint ini = new CvPoint(cvGetSeqElem(seq, 0));
		int leftmost = ini.x();
		int rightmost = ini.x();
		int topmost = ini.y();
		int bottommost = ini.y();
		for (int i = 0; i < seq.total(); i++) 
		{
			CvPoint v = new CvPoint(cvGetSeqElem(seq, i));
			if(leftmost > v.x())
				leftmost = v.x();
			if(rightmost < v.x())
				rightmost = v.x();
			if(topmost < v.y())
				topmost = v.y();
			if(bottommost > v.y())
				bottommost = v.y();
		}
		this.left = leftmost;
		this.right = rightmost;
		this.bottom= bottommost;
		this.top = topmost;
		
}
	
	public CvSeq getBallContour() {
		return ballContour;
	}
	public int getLeft() {
		return left;
	}
	public int getRight() {
		return right;
	}
	public int getTop() {
		return top;
	}
	public int getBottom() {
		return bottom;
	}
	public int getCenterX() {
		return centerX;
	}
	public int getCenterY() {
		return centerY;
	}
	public double getRadius() {
		return radius;
	}
	public boolean isInside(int x, int y) {
		return x >= this.getLeft() && x <= this.getRight() && y >= this.getBottom() && y <= this.getTop(); 
	}
	@Override
	public int compareTo(Ball ball) {
		if (this.getCenterX() < ball.getCenterX() ) return -1;
		else if (this.getCenterX() > ball.getCenterX()) return 1;
		else return 0;
	}


}
